var moduleLocal = [],
  pageScrollTimeOut = null

Page({
  /**
   * 页面的初始数据
   */
  data: {
    curTab: 0,
    modules: [{
        title: "utils 工具类",
        list: [{
            title: "$common",
            desc: "公共常用方法",
            url: "/pages/utilsCommon/utilsCommon",
            list: [{
                title: "digit",
                desc: "前置补0"
              },
              {
                title: "setting",
                desc: "打开授权弹窗"
              },
              {
                title: "toast",
                desc: "基于wx.showToast的封装"
              }
            ]
          },
          {
            title: "$time",
            desc: "时间处理",
            url: "/pages/utilsTime/utilsTime",
            list: [{
                title: "timeToDateObj",
                desc: "时间戳转换为对象"
              },
              {
                title: "formateTime",
                desc: "格式化时间戳"
              },
              {
                title: "getTimeCounter",
                desc: "获取当前时间到指定时间戳的倒计时"
              }
            ]
          }
        ]
      },
      {
        title: "canvas相关",
        list: [{
          title: "$css2canvas",
          desc: "接近css语法的canvas封装",
          url: "/pages/css2canvas/css2canvas",
          list: [{
              title: "initCanvas",
              desc: "初始化canvas"
            },
            {
              title: "roundRect",
              desc: "绘制圆角/直角矩形"
            },
            {
              title: "drawText",
              desc: "绘制普通文本"
            },
            {
              title: "drawImage",
              desc: "绘制图片"
            },
            {
              title: "drawTagNumber",
              desc: "绘制前置字体文本"
            },
            {
              title: "getWidth",
              desc: "获取指定文本宽度"
            }
          ]
        }]
      },
      {
        title: "组件封装",
        list: [{
          title: "<list-bottom />",
          desc: "好看的底部加载栏",
          url: "/pages/utilsCommon/utilsCommon"
        }]
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 记录节点位置
    wx.createSelectorQuery()
      .selectAll(".modules")
      .boundingClientRect()
      .exec(res => {
        res[0].forEach((item, index) => {
          moduleLocal[index] = item.top
        })
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},

  /**
   * 监听页面滚动，切换对应商品tab
   */
  onPageScroll: function(e) {
    let scrollTop = e.scrollTop
    for (let i = 0; i < moduleLocal.length; i++) {
      if (
        scrollTop >= moduleLocal[i] &&
        scrollTop < moduleLocal[i + 1] &&
        i != this.data.curTab &&
        !this.isClickClass
      ) {
        this.setData({
          curTab: i
        })
      } else if (
        scrollTop >= moduleLocal[i] &&
        i == moduleLocal.length - 1 &&
        !this.isClickClass &&
        i != this.data.curTab
      ) {
        this.setData({
          curTab: i
        })
      }
    }
  },

  /**
   * 修改当前选中的选项
   */
  changeNav: function({
    currentTarget
  }) {
    let {
      index
    } = currentTarget.dataset
    if (this.data.curTab != index) {
      this.isClickClass = true
      clearTimeout(pageScrollTimeOut)
      this.setData({
        curTab: index
      })

      wx.pageScrollTo({
        scrollTop: moduleLocal[index]
      })

      pageScrollTimeOut = setTimeout(() => {
        this.isClickClass = false
      }, 1000)
    }
  }
})